package com.bao.demo.utils;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @author baoye
 * @date 2024-08-22/14:27
 */
public class JDBCUtils {
    public static String MYSQL_DRIVER;
    public static String URL;
    public static String USERNAME;
    public static String PASSWORD;

    static {
        InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties");
        Properties prop = new Properties();
        try {
            prop.load(is);
            MYSQL_DRIVER = (String) prop.get("className");
            Class.forName(MYSQL_DRIVER);
            USERNAME = (String) prop.get("username");
            URL = (String) prop.get("url");
            PASSWORD = (String) prop.get("password");
        } catch (Exception e) {
            e.printStackTrace();

        }
    }


    public static Connection getConnection() {
        try {
            return DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public static Statement createStatement(Connection connection) {
        try {
            return connection.createStatement();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public static PreparedStatement createStatement(Connection connection, String sql, Object... o) {
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        int length = o.length;
        if (length == 0) {
            try {
                return connection.prepareStatement(sql);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        for (int i = length; i > 0; i--) {
            try {
                assert preparedStatement != null;
                preparedStatement.setObject(i, o[i - 1]);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return preparedStatement;
    }

    public static ResultSet getRes(Statement statement, String sql) {
        try {
            return statement.executeQuery(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public static ResultSet getRes(PreparedStatement statement) {
        try {
            return statement.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public static void close(Statement s, Connection c) {
        if (s != null) {
            try {
                s.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (c != null) {
            try {
                c.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static void close(ResultSet r, Statement s, Connection c) {
        if (r != null) {
            try {
                r.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        close(s, c);
    }
}
